<!DOCTYPE html>
<!-- 
  Usage: print.html?fns=FILENAME[||MORE_FILENAME][&dnb=true][&jt=JOB_TITLE][&as=ture]
  dnb:  不要分页，默认为分页，如果指示是否在不同文件之间分页
  jt:   作业标题，默认为没有，如果有则在所有内容签名打出title及时间
  as:   增加序号，默认不增加，如果启用则在打印所有文件前先输出下载序号
  nap:  自动打印，默认不自动，如果设为true则只显示，不自动打印
-->
<html>

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
    <link rel="stylesheet" type="text/css" href="http://127.0.0.1:8000/c_c2.css" />
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"> </script>
    <script src="http://127.0.0.1:8000/CLodopfuncs.js"></script>
    <title>Foodtrust Order Print Service</title>
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.3.0/weui.min.css"/>
    <style>
      body { width: 35em; margin: 0 auto; background-color: #FFFFFF; font-size: 14px; }
      a { color: #88B04B; }
      .center{ text-align: center; }
    </style>
  </head>

  <body>

    <h2 class="center">FOODTRUST订单随心打服务</h2>

    <div class="center">
      <span style="color: gray;">
        文件数：<span id="total-count"></span>&nbsp;&nbsp;已载入：<span id="downloaded-count"></span>&nbsp;&nbsp;<span id="status-message"></span>
      </span>
      <span id="reprint-button">
        <a  href="javascript:printOrders();" style="float: right; color: #88B04B;">打印内容</a>
      </span>
      <span style="display: none;" id="download-clodop">
        <a  href="javascript:window.open('http://www.c-lodop.com/download/CLodop_Setup_for_Win32NT_4.088Extend.zip');" style="float: right;">下载C-Lodop</a>
      </span>
    </div>

    <div>&nbsp;</div>

    <div id="order-contents"></div>

    <div id="footer">
      <div class="center" style="color: gray"> 本网站数据不提供直接访问，请通过<a href="https://foodtrust.cn/">FOODTRUST</a>相关服务获取<br></div>
      <br>
      <div class="center"><img src="https://mall.foodtrust.cn/h5/img/support.9e892abd.png" style="margin: 0 auto; width: 128px;"/></div>
    </div>

    <script>
      let value = getQueryString('fns') || ["FC4-DEMO.txt"];
      let doNotBreak = getQueryString('dnb')=='true' || false; //设置dnb=true时打印时每个文件不分页
      let addSerial = getQueryString('as' )=='true' || false; //设置as==true时候在每条记录签名加上数字
      let nap = getQueryString('nap') == 'true' || false;
      let title = getQueryString('jt'); //这是所有内容前面的标题
      let fns = value.split("||");
      let total_count = fns.length;
      let downloaded_count = 0;
      function getLodop() {
        var LODOP;
        try {
          LODOP = getCLodop();
          if (!LODOP && document.readyState !== 'complete') {
            alert('C-Lodop打印控件还没准备好，请稍后再试！')
            $("#reprint-button").hide();
            $("#download-clodop").show();
            return;
          }
          return LODOP
        } catch (err) {
          alert('请下载打印控件，安装成功后即可进行打印！')
          $("#reprint-button").hide();
          $("#download-clodop").show();
        }
      };
      function fetchContents(fns) {
        console.log("into fetch",fns);
        if (title) $("#order-contents").append( "<div style='font-size: 14px; font-weight:bold;' id='title'>" + title + " " + (new Date()).toLocaleString() + "</div>");
        for(var i=0; i<fns.length; i++){
          $.ajax({url:"/fc4-orders/2020/"+fns[i],success:function(content){
            let page = content.split("\n").join("<br>");
            downloaded_count = downloaded_count + 1;
            $("#downloaded-count").text(downloaded_count);
            var brk = doNotBreak ? "" : "<p style='page-break-after:always'>&nbsp;</p>";
            var serial = addSerial ? downloaded_count+" " : "";
            $("#order-contents").append("<div style='font-size: 14px' id='order-"+(new Date).getTime()+"'>"+serial+page+brk+"</div>");
            if (downloaded_count == total_count) {
              if (! nap) {
                $("#status-message").text('启动预览...');
                printOrders();
              };
              $("#status-message").text('');
            }
          }});
        }
      };
      function getQueryString(name) {
        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        let r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return decodeURIComponent(r[2]);
        };
        return null;
      };
      function printOrders() {
        LODOP=getLodop();
        LODOP.PRINT_INIT("FC4多订单打印");
        LODOP.SET_PRINT_PAGESIZE(0,'100mm','180mm','FC180'); //0是默认, 1代表横向打印，宽度固定，高度按打印内容的高度自适应,纸张100*180
        LODOP.ADD_PRINT_HTM('2mm','2mm','96mm','176mm',document.getElementById("order-contents").innerHTML);
        LODOP.PREVIEW();
      }
      $("#total-count").text(fns.length);
      $("#downloaded-count").text(downloaded_count);
      getLodop();
      fetchContents(fns);
    </script>
  </body>
</html>
